package com.test;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.excel.Points;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;

/**
 * Main class
 *
 * @author fs
 * @date 2021/04/21
 */
@Slf4j
public class Main {
    private static ConcurrentLinkedQueue<String> needToMoveList = new ConcurrentLinkedQueue();

    public static void main(String[] args) throws Throwable {
        for (int i =65;i< 128;i++) {
            System.out.println((char)i);
        }
//        getCustomerNoFromTxt("C:\\Users\\admin\\Desktop\\failData.txt");
//        parallel1();

//        System.out.println(new Random().nextInt(50));
//        System.out.println(new Random().nextInt(50));
//        System.out.println(new Random().nextInt(50));
//        System.out.println(new Random().nextInt(50));
//        System.out.println(new Random().nextInt(50));

//        readYztInsert("c:\\Users\\zhangdan\\Downloads\\yztinsert.txt");
//        readTxt("D:\\desktop\\统一客户\\多发的积分.txt");
//
//
//        System.out.println("ZZ123".matches("^[A-Z]+\\d+"));
//        System.out.println("HT123".matches("^[A-Z]+\\d+"));
//        System.out.println("H123".matches("^[A-Z]+\\d+"));
//        System.out.println("a123".matches("^[A-Z]+\\d+"));
//        System.out.println("123".matches("\\d+"));
//        System.out.println("a123".matches("\\d+"));
//        ZoneId zone = ZoneId.systemDefault();
//        System.out.println(zone); // Asia/Shanghai
        /*getFullPath("C:\\Users\\zhangdan\\Desktop\\richgo-redis-api");

        if (needToMoveList.isEmpty()) {
            System.out.println("没有需要移动的文件");
            return;
        }
        PrintWriter pw = new PrintWriter("C:\\Users\\zhangdan\\Desktop\\richgo-redis-api\\move.txt");
        needToMoveList.forEach(o -> {
            pw.append(o).append("\r\n");
        });
        pw.flush();*/

//        System.out.println(replaceTemplate("{0},{1}", new String[]{"卡号", "验证码"}));
//        System.out.println(replaceTemplate("{1},{0}", new String[]{"卡号", "验证码"}));
    }

    public static void parallel1() {
        List<Points> points = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            points.add(new Points("" + i, i, i));
        }
        points.parallelStream().forEach(p -> log.info(p.toString()));
    }

    public static void parallel() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Calendar> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Calendar startDay = new GregorianCalendar();
            Calendar checkDay = new GregorianCalendar();
            checkDay.setTime(startDay.getTime()); //不污染入参
            checkDay.add(Calendar.DATE, i);
            list.add(checkDay);
            checkDay = null;
            startDay = null;
        }
        list.stream().forEach(day -> System.out.println(sdf.format(day.getTime())));
        System.out.println("-----------------------");
        list.parallelStream().forEach(day -> System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(day.getTime())));
        System.out.println("-----------------------");
    }

    private static String replaceTemplate(String template, String[] args) {
        for (int i = 0; i < args.length; i++) {
            String target = "{" + i + "}";
            template = template.replace(target, args[i]);
        }
        return template;
    }

    private static void getFullPath(String basePath) throws Throwable {
        List<Path> fileList = Files.list(Paths.get(basePath)).collect(Collectors.toList());
        for (Path path : fileList) {
            if (Files.isDirectory(path)) {
                getFullPath(path.toString());
                continue;
            }
            if (path.getFileName().toString().contains("SNAPSHOT")) {
                System.out.println(path.getFileName().toAbsolutePath().toString());
                needToMoveList.add(path.getFileName().toAbsolutePath().toString());
            }
        }
    }

    private static void readSql(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        Scanner scanner = new Scanner(path);
        System.out.println("Read text file using Scanner");
        // 一行一行地读取
        String optType;
        String customerNo;
        int customerPoint = 0;
        while (scanner.hasNextLine()) {
            //process each line
            String line = scanner.nextLine();
            optType = line.split(",")[0];
            customerNo = line.split(",")[1];
            customerPoint = Integer.valueOf(line.split(",")[2]);
            if (customerPoint > 9900) {
                for (int i = 9900; i < customerPoint; customerPoint = customerPoint - i) {
                    System.out.println(optType + "\t" + customerNo + "\t" + i);
                }
            }
            System.out.println(optType + "\t" + customerNo + "\t" + customerPoint);

        }
        scanner.close();
    }


    private static void readTxt(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        Scanner scanner = new Scanner(path);
        System.out.println("Read text file using Scanner");
        // 一行一行地读取
        String optType;
        String customerNo;
        int customerPoint = 0;
        while (scanner.hasNextLine()) {
            //process each line
            String line = scanner.nextLine();
            optType = line.split(",")[0];
            customerNo = line.split(",")[1];
            customerPoint = Integer.valueOf(line.split(",")[2]);
            if (customerPoint > 9900) {
                for (int i = 9900; i < customerPoint; customerPoint = customerPoint - i) {
                    System.out.println(optType + "\t" + customerNo + "\t" + i);
                }
            }
            System.out.println(optType + "\t" + customerNo + "\t" + customerPoint);

        }
        scanner.close();
    }

    // private static ObjectMapper om = new ObjectMapper();

    private static void getCustomerNoFromTxt(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        Scanner scanner = new Scanner(path);
        String customerNo = "";
        System.out.println("Read text file using Scanner");
        // 一行一行地读取
        Set<String> container = new HashSet<>();
        while (scanner.hasNextLine()) {
            //process each line
            String line = scanner.nextLine();
            if (StringUtils.isBlank(line) || !line.contains("方法入参")) {
                continue;
            }
            JSONObject jsonObject = JSONObject.parseObject(line.split("方法入参")[1]);
            JSONObject body = jsonObject.getJSONObject("req");
            customerNo = body.get("customerNo").toString();
            System.out.println(customerNo);
            container.add(customerNo);
        }
        scanner.close();
        System.out.println(container);
    }
    /**
     * 解析理顾绑定异常日志
     *
     * @param fileName
     * @throws IOException
     */
    private static void readYztInsert(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        Scanner scanner = new Scanner(path);
        System.out.println("Read text file using Scanner");
        // 一行一行地读取
        List<String> cer = new ArrayList<>();
        String inputParam = "";
        while (scanner.hasNextLine()) {
            //process each line
            String line = scanner.nextLine();
            if (line.contains("方法入参")) {
                inputParam = line;
            }
            if (line.contains("\"status\":\"4009\"")) {
                cer.add(inputParam.substring(inputParam.indexOf("方法入参") + 4));
            }
        }
        scanner.close();
        System.out.println(cer);
    }
}
